import grails.test.ControllerUnitTestCase

class TradesPersonControllerTests extends ControllerUnitTestCase{
    def controller
    Trade trade1, trade2
    def mockTradeService

    void setUp(){
        super.setUp()
        controller = new TradesPersonController()
        mockDomain TradesPerson, [new TradesPerson(personalName:'P1')]

        mockTradeService = mockFor(TradeService.class)
    }

    void testThatControllerSavesTradesPerson() {
        def mockControl = mockFor(TradesPerson.class)
        mockControl.demand.hasErrors(1..1){->return false}
        mockControl.demand.save(1..1){->return true}

        mockTradeService.demand.findOrCreateTrades(1..1){String str ->
            return [new Trade(tradeName:'plumber')]
        }

        controller.params.personalName = 'Glenn'
        controller.params.businessName = 'Top Gun'
        controller.params.headline = 'Best of the best'
        controller.params.body = 'The best carpenter around'
        controller.tradeService = new TradeService()
        controller.save()

        assertEquals('show', redirectArgs.action)
    }

    void testThatControllerSavesAndCreatesTrade() {
        def mockControl = mockFor(TradesPerson.class)
        mockControl.demand.hasErrors(1..1){->return false}
        mockControl.demand.save(1..1){ ->  return true }

        String tradeStr = 'carpenter, plumber'
        mockTradeService.demand.findOrCreateTrades(1..1){String str ->
            assertEquals(tradeStr, str)
            return [new Trade(tradeName:'plumber')]}

        controller.params.personalName = 'Glenn'
        controller.params.businessName = 'Top Gun'
        controller.params.headline = 'Best of the best'
        controller.params.body = 'The best carpenter around'
        controller.params.tradesStr = tradeStr
        controller.tradeService = new TradeService()
        controller.save()

        assertEquals('show', redirectArgs.action)
    }

    void testThatControllerHandlesWhenSaveFails(){
        def mockControl = mockFor(TradesPerson.class)
        mockControl.demand.hasErrors(1..1){->return false}
        mockControl.demand.save(1..1){ ->  return false }

        String tradeStr = 'carpenter, plumber'
        mockTradeService.demand.findOrCreateTrades(1..1){String str ->
            assertEquals(tradeStr, str)
            return [new Trade(tradeName:'plumber')]}

        controller.params.body = 'The best carpenter around'
        controller.params.tradesStr = tradeStr
        controller.tradeService = new TradeService()
        controller.save()

        assertEquals('create', renderArgs.view)
    }

    void testThatControllerHandlesWhenHasErrorsIsTrue(){
        def mockControl = mockFor(TradesPerson.class)
        mockControl.demand.hasErrors(1..1){->return true}
        mockControl.demand.save(0..0){ ->  return false }

        String tradeStr = 'carpenter, plumber'
        mockTradeService.demand.findOrCreateTrades(1..1){String str ->
            assertEquals(tradeStr, str)
            return [new Trade(tradeName:'plumber')]}

        controller.params.body = 'The best carpenter around'
        controller.params.tradesStr = tradeStr
        controller.tradeService = new TradeService()
        controller.save()

        assertEquals('create', renderArgs.view)
    }
}